import gtk

import sgf

# TODO:
# - make width fixed, so the dialog size doesn't jump around a lot?
# - interpret the result, so instead of "W+0.50", it's "White, by 0.5 points"
# - add a "Moves: %d" line (number of moves, following the A-variation all the way down)
# - docstrings!

class SGFPreview(gtk.Table):
    """a preview component for sgf files.  should look like this:
   Title: Championship Game
   Black: joe (5k)
   White: ed (6k)
Handicap: 2
    Komi: 5.5
  Result: Black, by 5
    Date: 1 July 2004

    usage: make your gtk.FileChooserDialog, then make a preview with
    preview = SGFPreview(dialog)
    that's it."""
    def __init__(self, file_chooser):
        gtk.Table.__init__(self, columns=2, rows=7)
        self.set_row_spacings(5)
        self.set_col_spacings(10)

        # (these next 2 lists must match!)
        labels = ["Title:", "Black:", "White:", "Handicap:", "Komi:", "Result:", "Date:"]
        self.keys = ["GN", "PB", "PW", "HA", "KM", "RE", "DT"]

        self.values = []

        for i,text in enumerate(labels):
            label = gtk.Label(text)
            label.set_alignment(xalign=1.0, yalign=0.0)
            self.attach(label, 0, 1, i, i+1, xoptions=gtk.FILL, yoptions=0)
            value = gtk.Label(u'\N{EM DASH}')
            value.set_alignment(xalign=0.0, yalign=0.0)
            self.values += [value]
            self.attach(value, 1, 2, i, i+1, yoptions=0)

        self.show_all()

        file_chooser.set_preview_widget(self)
        file_chooser.connect("update-preview", self.update_preview)

        file_chooser.set_preview_widget_active(False)

    def update_preview(self, file_chooser):
        filename = file_chooser.get_preview_filename()
        try:
            s = sgf.load(filename)

            for i,key in enumerate(self.keys):
                if key in s.head.properties:
                    value = s.head.properties[key]
                else:
                    value = u'\N{EM DASH}'
                self.values[i].set_text(value)

            have_preview = True
        except Exception:
            have_preview = False
        file_chooser.set_preview_widget_active(have_preview)

# ----------------------------------------

def make_dialog(title, action):
    # (put this in the .glade file?  doesn't seem to be any way to make
    # 'ok' the default in glade.)
    dialog = gtk.FileChooserDialog(title=title,
                                   action=action,
                                   buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                            gtk.STOCK_OK, gtk.RESPONSE_OK))

    dialog.set_property("do-overwrite-confirmation", True)
    dialog.set_default_response(gtk.RESPONSE_OK)

    # my spiffy preview
    preview = SGFPreview(dialog)

    dialog.set_icon_from_file("Resources/2-stones.png")

    filter = gtk.FileFilter()
    filter.set_name("Go games")
    filter.add_mime_type("application/x-go-sgf")
    filter.add_pattern("*.sgf")
    dialog.add_filter(filter)

    filter = gtk.FileFilter()
    filter.set_name("All files")
    filter.add_pattern("*")
    dialog.add_filter(filter)

    return dialog

# ----------------------------------------
def test():
    # NOTE: 'ok' doesn't do anything, and you can't ctrl-C it; ctrl-Z it, and 'kill %1'
    dialog = gtk.FileChooserDialog(title="Choose an SGF file", \
                                   buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, \
                                            gtk.STOCK_OK, gtk.RESPONSE_OK))
    dialog.set_default_response(gtk.RESPONSE_OK)
    dialog.set_icon_from_file("Resources/2-stones.png")
    SGFPreview(dialog)
    dialog.run()
#    gtk.main()
if __name__ == '__main__':
    test()
